/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-this-alias */
/* eslint-disable @typescript-eslint/no-unused-vars */
export const writeConfig = [
    {
        label: 'new',
        code: () => {
            function myNew(fn, ...args) {
                if (typeof fn !== 'function') {
                    return new TypeError('fn must be a function')
                }

                const obj = Object.create(fn.prototype)
                let result = fn.apply(obj, args)
                const flag = result && (typeof result === 'object' || typeof result === 'function')
                return flag ? result : obj
            }
        },
        desc: `
        有四个步骤：
        1.新建一个对象
        2.把原型链指向新对象
        3.修改this
        4.返回对象
        `
    },
    {
        label: 'instanceOf',
        code: () => {
            function myInstanceOf(obj, type) {
                let objPrototype = Object.getPrototypeOf(obj)

            }
        },
        desc: `

        `
    },
    {
        label: 'throttle',
        code: () => {
            // type1
            function throttle1(fn, wait) {
                let startTime = Date.now()

                return function () {
                    const nowTime = Date.now()
                    if (nowTime - startTime >= wait) {
                        startTime = nowTime
                        return fn.apply(this, arguments)
                    }
                }
            }
            // type2
            function throttle2(fn, wait) {
                let timer = null
                var _this = this;
                return function () {
                    if (timer) {
                        return
                    }
                    timer = setTimeout(() => {
                        fn.apply(_this, args);
                        timer = null
                    }, wait);
                }
            }
        },
        desc: `
        防抖：连续多次输入时只执行最后一次 通过定时器实现，声明一个定时器，逻辑放在定时器内部，触发时先清除定时器
        节流：连续输入时按频率输出：实现方式有两种 1.时间戳 2.定时器 都通过闭包报错内部变量
        时间戳:记录oldtime和nowtime，如果插值大于规定时间则执行回调，将now赋值给old
        定时器:声明一个timer变量,值为null,如果null有值直接return，没有值将定时器赋值给timer,定时器内部执行回调并将timer设为null
        `
    },
    {
        label: 'debounce',
        code: () => {
            // type1
            function debounce(fn, wait) {
                let timer
                const _this = this
                return function () {
                    clearTimeout(timer)
                    timer = setTimeout(() => {
                        fn.apply(_this, args)
                    }, wait);
                }
            }
            // type2
        },
        desc: ``
    },
    {
        label: '浅拷贝',
        code: () => {
            // type1
            function shaowCpoy1(obj) {
                const result = Object.assign({}, obj)
                return result
            }
            // type2
            function shaowCpoy2(obj) {
                const result = { ...obj }
                return result
            }
            // type3
            function shaowCpoy3(obj) {
                const newObj = Array.isArray(obj) ? [] : {}
                for (let index in obj) {
                    if (obj.hasProperty(key)) {
                        newObj[key] = obj[key]
                    }
                }
                return newObj
            }
        },
        desc: ``
    },
    {
        label: '深拷贝',
        code: () => {
            function deepCopy() {
                //  JSON.parse(JSON.stringify())
                // 缺陷：undefied,function,symbol,循环引用
            }
        },
        desc: ``
    },
    {
        label: 'Object.create',
        code: () => {
            function myCreate(obj) {
                let func = function () { }
                func.prototype = obj
                return new func
            }
        },
        desc: `

        `
    },
]